package com.wanchuang.qhbb.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;


import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.module.login.LoginActivity;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;

import java.util.List;
import java.util.Map;


public abstract class BaseActivity extends AppCompatActivity {

    private ProgressDialog loadingDialog;
    private TextView loading_tv;
    protected Context mContext;
    private View dialogLayout;
    private AlertDialog hintDialog;
    public boolean isDestory;
    private boolean isShowingTokanHint;
    private AlertDialog mTokenHintDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        MainApplication.addActivity(this);
        mContext = this;
    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    protected void onDestroy() {
        isDestory = true;
        hideProgress();
        if (hintDialog != null && hintDialog.isShowing()) {
            hintDialog.dismiss();
        }
        if (mTokenHintDialog != null && mTokenHintDialog.isShowing()) {
            mTokenHintDialog.dismiss();
        }
        MainApplication.removeActivity(this);
        super.onDestroy();
        System.gc();
    }



    /*------------------------简化请求------------------------*/
    public void requestPost(String api, Map<String, String> params, OnRequestCallBack callBack) {
        NetworkManager.INSTANCE.post(api, params, callBack);
    }
    public void requestGet(String api, Map<String, String> params, OnRequestCallBack callBack) {
        NetworkManager.INSTANCE.get(api, params, callBack);
    }




    /*------------------------页面跳转------------------------*/
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(getApplicationContext(), clz));
    }

    public void startActivityForResult(Class<?> clz, int requestCode) {
        startActivityForResult(new Intent(getApplicationContext(), clz), requestCode);
    }

    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }


    /*------------------------Toast方法------------------------*/
    public void showToast(String message) {
        Toast.makeText(mContext,message,Toast.LENGTH_SHORT).show();
    }

    /*------------------------弹窗提示------------------------*/
    public void showHintDialog(String message) {
        if (isFinishing()) return;
        if (hintDialog == null) {
            androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(mContext);
            builder.setMessage(message);
            builder.setPositiveButton("确定", null);
            hintDialog = builder.show();
        } else {
            hintDialog.setMessage(message);
            hintDialog.show();
        }
    }

    /*------------------------加载提示 loading 方法------------------------*/
    public void showProgress(String message) {
        showProgress(message, true);
    }

    public void showProgress() {
        showProgress("请稍后...", true);
    }

    public void showProgress(boolean isCancelable) {
        showProgress("请稍后...", isCancelable);
    }

    public void showProgress(String message, boolean isCancelable) {
        if (loadingDialog == null) {
            LayoutInflater inflater = LayoutInflater.from(this);
            // 得到加载view
            dialogLayout = inflater.inflate(R.layout.layout_view_loading, null);
            loading_tv = (TextView) dialogLayout.findViewById(R.id.tv_load_dialog);
            loadingDialog = new ProgressDialog(this, R.style.transparent_dialog_theme);
            loadingDialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
            loadingDialog.setCancelable(isCancelable);
            loadingDialog.setCanceledOnTouchOutside(false);
            loadingDialog.show();
            loadingDialog.setContentView(dialogLayout);// 设置布局 需要在show后执行 否则报requestFeature() must be called before adding content
            loadingDialog.setOnDismissListener(dialog -> onLoadingDismiss());
        }
        loading_tv.setText(message);
        if (getBaseContext() != null && !loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }


    public void onLoadingDismiss() {

    }

    public void hideProgress() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }


    /*------------------------ 验证码倒计时方法------------------------*/
    public void countDownReSend(final TextView textView, long sec) {
        if (textView == null) return;
        showToast("验证码发送成功，请注意查收");
        textView.setTag(textView.getTextColors());
        textView.setEnabled(false);
        new CountDownTimer(sec * 1000, 1000) {
            public void onTick(long millisUntilFinished) {
                if (isDestory) {
                    cancel();
                    return;
                }
                textView.setText((millisUntilFinished / 1000) + "秒倒计时");
            }

            public void onFinish() {
                if (isDestory)
                    return;
                textView.setText("重新获取");
                textView.setEnabled(true);
                textView.setSelected(false);
            }
        }.start();
    }

    /*------------------------弹出登录失效弹窗------------------------*/
    public void showTokenInvalidHintDialog() {
        if (isShowingTokanHint) return;
        if (isFinishing()) return;
        isShowingTokanHint = true;
        androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(mContext);
        builder.setTitle("提示");
        builder.setMessage("您的登录凭证已失效，请重新登录！");
        builder.setCancelable(false);
        builder.setOnDismissListener(dialog -> {
            isShowingTokanHint = false;
        });
        builder.setNegativeButton("", (dialog, which) -> {

        });
        builder.setPositiveButton("登录", (dialog, which) -> {
            startActivity(LoginActivity.class);
        });
        mTokenHintDialog = builder.show();
    }

    /*------------------------校验方法------------------------*/
    protected boolean isEmpty(String value) {
        return TextUtils.isEmpty(value);
    }

    protected <T> boolean isEmpty(List<T> value) {
        return value == null || value.size() == 0;
    }

    protected <T> boolean isEmpty(Map<T, T> value) {
        return value == null || value.size() == 0;
    }

    protected boolean isNull(Object o) {
        return o == null;
    }



}
